<?php

namespace app\uniapp\service;

use app\uniapp\controller\UniController;
use app\uniapp\service\traits\InstanceTrait;
use think\facade\Cache;
use think\route\Rule;

class SmsService extends UniBaseService
{

    use InstanceTrait;

    public function validateCode($code, $mobile): bool
    {
        $key = $this->makeSmsKey($this->siteId, $mobile);
        $cacheCode = Cache::get($key, '');
        if ($code && $cacheCode == $code){
            // 删除验证码
            Cache::delete($key);
            return true;
        }

        return false;

    }

    public function makeCodeKey($mobile): string
    {
        return "sms_{$this->siteId}_{$mobile}";
    }

    public function saveCode($code, $mobile){
        $key = SmsService::instance()->makeCodeKey($mobile);
        Cache::set($key, $code, 180);
    }

    function initialize()
    {
        // TODO: Implement initialize() method.
    }

    public function send(){
        // todo: 发送短信
    }

    function formatData(array $data): array
    {
        return  $data;
    }
}
